package com.coffee.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.api.merchant.query.MerchantInvoiceQuery;
import com.coffee.admin.api.merchant.result.MerchantInvoiceResult;
import com.coffee.admin.api.merchant.service.MerchantInvoiceService;
import com.coffee.core.anotation.LoginCheck;
import com.coffee.core.anotation.OperLog;
import com.coffee.core.entity.LoginUser;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.result.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/merchant/invoice")
public class MerchantInvoiceController extends BaseController {
    private static final Logger LOG = LoggerFactory.getLogger(MerchantInvoiceController.class);

    @DubboReference
    private MerchantInvoiceService merchantInvoiceService;


    @OperLog(operModul = "商户-商户发票列表", operDesc = "商户发票列表")
    @LoginCheck
    @RequestMapping(value = "/list", method = {RequestMethod.POST})
    public Result<List<MerchantInvoiceResult>> findList(HttpServletRequest request) {
        LoginUser merchant = getMerchant();
        Result<List<MerchantInvoiceResult>> result = Result.success(null);
        MerchantInvoiceQuery param = new MerchantInvoiceQuery();
        param.setMerchantId(merchant.getUserId());
        List<MerchantInvoiceResult> list = merchantInvoiceService.getList(param);
        result.setData(list);
        return result;
    }


    @OperLog(operModul = "商户-商户发票详情", operDesc = "商户发票详情")
    @LoginCheck
    @RequestMapping(value = "/detail", method = {RequestMethod.POST})
    public Result<MerchantInvoiceResult> detail(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        LoginUser merchant = getMerchant();
        Result<MerchantInvoiceResult> result = Result.success(null);
        Integer id = jsonObject.getInteger("id");
        MerchantInvoiceQuery param = new MerchantInvoiceQuery();
        param.setMerchantId(merchant.getUserId());
        MerchantInvoiceResult detail = merchantInvoiceService.getDetail(id, merchant.getUserId());
        result.setData(detail);
        return result;
    }


    @OperLog(operModul = "商户-商户发票详情", operDesc = "商户发票详情")
    @LoginCheck
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    public Result<String> delete(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        LoginUser merchant = getMerchant();
        Result<String> result = Result.success(null);
        Integer id = jsonObject.getInteger("id");
        MerchantInvoiceQuery param = new MerchantInvoiceQuery();
        param.setMerchantId(merchant.getUserId());
        merchantInvoiceService.delete(id, merchant.getUserId());
        return result;
    }

    @OperLog(operModul = "商户-商户发票详情", operDesc = "商户发票详情")
    @LoginCheck
    @RequestMapping(value = "/addOrUpdate", method = {RequestMethod.POST})
    public Result<String> add(HttpServletRequest request, @RequestBody MerchantInvoiceResult merchantInvoiceResult) {
        LoginUser merchant = getMerchant();
        Result<String> result = Result.success(null);
        merchantInvoiceResult.setMerchantId(merchant.getUserId());
        MerchantInvoiceQuery param = new MerchantInvoiceQuery();
        param.setMerchantId(merchant.getUserId());
        try {
            merchantInvoiceService.addOrUpdate(merchantInvoiceResult);
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.failed(e.getCode(), e.getMessage());
        }
        result.setData("SUCCESS");
        return result;
    }


}

